package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"order-exam/cache"
	"order-exam/model"
	"strconv"
	"time"
)

func CatAdd(c *gin.Context) {
	catName := c.PostForm("catName")
	catContent := c.PostForm("catContent")

	if catName == "" || catContent == "" {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "不能为空",
		})
		return
	}

	cat := model.Cat{
		CatName:    catName,
		CatContent: catContent,
	}

	err := model.DB.Create(&cat).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderAdd(c *gin.Context) {
	orderName := c.PostForm("orderName")
	orderContent := c.PostForm("orderContent")
	orderPrice, _ := strconv.Atoi(c.PostForm("orderPrice"))
	cid, _ := strconv.Atoi(c.PostForm("cid"))

	order := model.Order{
		OrderName:    orderName,
		OrderContent: orderContent,
		OrderPrice:   orderPrice,
		Cid:          cid,
	}

	err := model.DB.Create(&order).Error
	if err != nil {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "添加失败",
		})
		return
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "添加成功",
	})
	return
}

func OrderList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "5"))
	offset := (page - 1) * size

	var order []model.CatOrder
	/*
		SELECT
		orders.order_name AS OrderName,
		orders.order_content AS OrderContent,
		orders.order_price AS OrderPrice,
		cats.cat_name AS CatName,
		cats.cat_content AS CatContent
		FROM
		cats
		LEFT JOIN orders ON orders.cid=cats.id
		WHERE
		orders.cid=1
	*/
	redis_key := "order_list" + strconv.Itoa(page)
	redisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()

	if redisData == "" {
		model.DB.Table("cats").
			Select(
				"orders.order_name AS OrderName",
				"orders.order_content AS OrderContent",
				"orders.order_price AS OrderPrice",
				"cats.cat_name AS CatName",
				"cats.cat_content AS CatContent",
			).
			Joins("LEFT JOIN orders ON orders.cid=cats.id").
			Debug().
			Offset(offset).
			Limit(size).
			Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(redisData), &order)
	}

	var total int64
	model.DB.Model(&model.Order{}).Count(&total)

	c.JSON(200, gin.H{
		"code":  200,
		"msg":   "ok",
		"data":  order,
		"total": total,
	})
	return

}

func OrderDetail(c *gin.Context) {
	id := c.Query("id")
	var order []model.CatOrder
	/*
		SELECT
		orders.order_name AS OrderName,
		orders.order_content AS OrderContent,
		orders.order_price AS OrderPrice,
		cats.cat_name AS CatName,
		cats.cat_content AS CatContent
		FROM
		cats
		LEFT JOIN orders ON orders.cid=cats.id
		WHERE
		orders.cid=1
	*/
	redis_key := "order_detail" + id
	redisData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()

	if redisData == "" {
		model.DB.Table("cats").
			Select(
				"orders.order_name AS OrderName",
				"orders.order_content AS OrderContent",
				"orders.order_price AS OrderPrice",
				"cats.cat_name AS CatName",
				"cats.cat_content AS CatContent",
			).
			Joins("LEFT JOIN orders ON orders.cid=cats.id").
			Where("orders.cid=?", id).
			Debug().
			Find(&order)
		jsonData, _ := json.Marshal(order)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(redisData), &order)
	}

	c.JSON(200, gin.H{
		"code": 200,
		"msg":  "ok",
		"data": order,
	})
	return

}
